/*
 *所有关于upper_tool类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*upper_toolService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.System.IRepositories;
using Microsoft.AspNetCore.SignalR;
using System.Collections.Generic;
using SkiaSharp;
using NPOI.Util.Collections;
using VOL.Core.Enums;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace VOL.System.Services
{
    public partial class upper_toolService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Iupper_toolRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public upper_toolService(
            Iupper_toolRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        public int ChangeToolStatus(int id,int location,int status=1)
        {
            if (location==0)
            {
                return -1;
            }
            _repository.DbContext.QueryTracking = true;
            var tool = _repository.DbContext.Set<upper_tool>().Where(a => a.id == id).FirstOrDefault();
            if (tool!=null)
            {
                tool.use_status = status;
                tool.location = location;
                _repository.DbContext.Entry(tool).State = EntityState.Modified;
                return _repository.DbContext.SaveChanges();
            }
            return -1;
        }
        /*
        public override PageGridData<upper_tool> GetPageData(PageDataOptions options)

        {
            //查询table界面显示求和
            JArray jArray = JArray.Parse(options.Wheres);
            IEnumerable<upper_tool> queryable;
            if (jArray.Count > 0)
            {
                var j= jArray[0]["value"].ToString();
                queryable = (from t in _repository.DbContext.Set<upper_tool>()
                             join x in _repository.DbContext.Set<upper_work_order>()
                             on t.work_order_id equals x.work_order_id
                                 //  orderby o.process_Status == 1 ? 0 : 1, o.process_Status,t.work_order_id
                             where 
                             x.work_order_number== j


                             orderby t.use_status, t.work_order_status == 1 ? 0 : 1, t.work_order_status, t.work_order_id, t.program_seq
                             //orderby t.use_status
                             select t); 
            }
            else
            {
               
                queryable = (from t in _repository.DbContext.Set<upper_tool>()
                                 //  orderby o.process_Status == 1 ? 0 : 1, o.process_Status,t.work_order_id
                             orderby t.use_status, t.work_order_status == 1 ? 0 : 1, t.work_order_status, t.work_order_id, t.program_seq
                             //orderby t.use_status
                             select t);

            }



            PageGridData<upper_tool> pageGridData = new PageGridData<upper_tool>();
            //if (QueryRelativeExpression != null)
            //{
            //    queryable = QueryRelativeExpression.Invoke(queryable);
            //}
            //if (options.Export)
            //{
            //    if (Limit > 0)
            //    {
            //        queryable = queryable.Take(Limit);
            //    }
                
            //}
            //else
            {
                pageGridData.rows = queryable.ToList();
                //pageGridData.rows = repository.IQueryablePage(queryable,
                //                    options.Page,
                //                    options.Rows,
                //                    out int rowCount,
                //                    new Dictionary<string, QueryOrderBy>() { { "aaaa", QueryOrderBy.Asc } }

                //                    ).ToList();
                pageGridData.total = queryable.ToList().Count;
                //查询界面统计求等字段
                if (SummaryExpress != null)
                {
                    pageGridData.summary = SummaryExpress.Invoke((IQueryable<upper_tool>)queryable);
                    //Func<T, T> groupExpress = x =>x;
                    //pageGridData.summary = queryable.GroupBy(groupExpress).Select(SummaryExpress).FirstOrDefault();
                }
            }
            GetPageDataOnExecuted?.Invoke(pageGridData);
            return pageGridData;

        }
        */
    }

}
